package com.hfmall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hfmall.common.ClassDto;
import com.hfmall.entity.HfClass;
import com.hfmall.entity.ResponseCode;
import com.hfmall.service.impl.HfClassServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liclo
 * @since 2021-08-12
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/class")
public class HfClassController {
    @Autowired
    private HfClassServiceImpl hfClassService;

    /**
     * 根据分类id查询规格
     *
     * @author liclo
     * @date 2021/8/16
     */
    @RequestMapping("getclass")
    public ResponseCode getClass(Integer cid) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("category_id", cid);
        return new ResponseCode(hfClassService.list(wrapper));
    }

    @GetMapping("getAll")
    public ResponseCode getAll() {
        return new ResponseCode(hfClassService.getAllClass(0));
    }

    @PostMapping("add")
    public ResponseCode add(@RequestBody ClassDto classDto) {
        log.debug("categoryId:{}", classDto);
        HfClass hfClass = new HfClass();
        hfClass.setCategoryId(classDto.getCategoryId());
        classDto.getClassName().forEach(e -> {
            hfClass.setClassName(e);
            hfClass.setId(0);
            hfClassService.save(hfClass);
        });
        return ResponseCode.SUCCESS;
    }
}
